// 浏览器看到的结果
#include "searcher.hpp"
#include "cpp-httplib/httplib.h"
#include "log.hpp"

const std::string root_path = "./wwwroot"; // 存放网页资源的根目录
const std::string input = "data/raw_html/raw.txt";

int main()
{
    ns_searcher::Searcher search;
    search.InitSearcher(input);
    // http,http url格式中?后面是参数
    httplib::Server svr;

    svr.set_base_dir(root_path.c_str());
    // lambda表达式可以省略参数名
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &res)
            {
                if (!req.has_param("word"))
                {
                    res.set_content("搜索必须要有搜索关键字!", "text/plain; charset=utf-8");
                    return;
                }
                std::string word = req.get_param_value("word");
                // std::cout << "用户正在搜索: " << word << std::endl;
                LOG(NORMAL, "用户搜索的: " + word);
                std::string json_string;
                search.Search(word, &json_string);
                res.set_content(json_string, "application/json"); // 这里的http网页内容格式可以查询http content-type，text/plain就是普通文本类型,application/json就是json类型
                // res.set_content("hello world!", "text/plain; charset=utf-8");// Get("/hi")发送hi请求，回应一个hello world
            });
    svr.listen("0.0.0.0", 8081);
    return 0;
}